# Install dependencies only when needed
FROM node:18-alpine AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY . .
RUN npm install -g pnpm && pnpm i --ignore-scripts && npm run build:test;

RUN apk add --no-cache wget && wget https://gosspublic.alicdn.com/ossutil/1.7.13/ossutil64 && chmod 755 ossutil64 && ./ossutil64 config -e oss-cn-shanghai.aliyuncs.com -i yourSecret -k yourKey && ./ossutil64 -v
RUN ./ossutil64 cp /app/dist/ oss://xxx-bucket/static-web/test/ --meta x-oss-object-acl:public-read  -r -f

# Production image, copy all the files and run nuxt
FROM nginx:1.24.0-alpine AS runner
WORKDIR /app

#COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.template /app
# COPY --from=builder /app/dist/. /usr/share/nginx/html/

EXPOSE 80

ENV CDN_NAME test
ENV CDN_URL https://cdn.example.com
ENV CDN_URL_PATH $CDN_URL/static-web/$CDN_NAME

ENTRYPOINT envsubst '${CDN_URL} ${ENV_NAME} ${CDN_URL_PATH}' < nginx.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'
